package com.coocaa.ops.plus.dashboard.v1.codegen;

import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.tool.json.JsonUtil;
import com.coocaa.ops.plus.dashboard.v1.codegen.handler.CodeGenDataHandler;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.CodeGenConfigModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.TableInfoModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.query.DbTableQueryModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.service.ICodeGenService;
import com.coocaa.ops.plus.dashboard.v1.codegen.validator.CodeGenValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @author : bijiahao
 * @date : 2019/4/19.
 * @description:
 */
@RestController
@RequestMapping("/v1/codeGen")
public class CodeGenController{

    @Autowired
    private ICodeGenService codeGenService;

    @RequestMapping(value = "/execCodeGen", produces = "text/html;charset=UTF-8")
    public R execCodeGen(HttpServletRequest request, @RequestParam String jsonStr) throws Exception {
        CodeGenConfigModel codeGenConfigModel = (CodeGenConfigModel) JsonUtil.json2Object(jsonStr, CodeGenConfigModel.class);
        CodeGenValidator.valid(codeGenConfigModel);
        //代码生成之前的处理
        CodeGenDataHandler.handleBeforeCodeGen(codeGenConfigModel, request);
        return codeGenService.execCodeGen(codeGenConfigModel);

    }

    /**
     * 获取配置信息
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/configInfo", produces = "text/html;charset=UTF-8")
    public R configInfo(HttpServletRequest request) throws Exception {
        return R.ok().data(codeGenService.configInfo(request));
    }


    @RequestMapping(value = "/listTable", produces = "text/html;charset=UTF-8")
    public R create(HttpServletRequest request, DbTableQueryModel model) throws Exception {
        PageResultModel<TableInfoModel> pageResult = codeGenService.listTable(model);
        return R.ok(pageResult);
    }


    @RequestMapping(value = "/testDbConn")
    public R testDbConn(HttpServletRequest request, DbTableQueryModel model) {
        return codeGenService.testDbConn(model);
    }
}
